/*
 * Sprite.cpp
 * Any sprite. Sprites have a location, an image, and an outline.
 */

#include "Sprite.h"

// There are two ways to make a sprite:
//	1. We start off with an image path, a width, and a height. 
//	2. For convenience while developing, you can define a sprite
//		as having a width, height and colour. We'll draw a
//		simple box.
Sprite::Sprite(std::string imagePath) {
	this->image = 	new CL_Surface(RESOURCES_PATH + imagePath);

	x = 	0;
	y = 	0;
	w = 	this->image->get_width();
	h = 	this->image->get_height();

	this->colour =	NULL;
}

Sprite::Sprite(CL_Color& colour, int x, int y, int width, int height) {
	this->x = 	x;
	this->y =	y;
	this->w =	width;
	this->h =	height;

	this->colour = 	new CL_Color(colour);
	this->image =	NULL;
}

// Draw this onto the CL_Display.
void Sprite::paint() {
	if(image != NULL) {
		image->draw(x, y);
	} else if(colour != NULL) {
		CL_Rectf rect(
			(float) x, 
			(float) y,
			(float) x + w,
			(float) y + h
		);
		CL_Display::fill_rect(rect, *colour);

		#define DARKEN(x) ((x) > 230 ? (x) + 20 : (x))

		CL_Color colour_dark(
			DARKEN(colour->get_red()),
			DARKEN(colour->get_green()),
			DARKEN(colour->get_blue())
		);

		CL_Display::draw_rect(rect, colour_dark);
	}
}

// Destroy this world.
Sprite::~Sprite() {
	if(image != NULL)
		delete image; 
	if(colour != NULL)
		delete colour;
}
